<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>选择社区</title>
		<link rel="stylesheet" type="text/css" href="../../css/hui.css" />
		<link rel="stylesheet" type="text/css" href="../../css/common.css" />
		<link rel="stylesheet" type="text/css" href="../../css/communitycity.css" />
		<script src="../../js/common/flexible_css.debug.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.6&key=0ed412bf3788281d7bfdcc420beedca1&plugin=AMap.Geocoder"></script>
		<!--<script src="../js/common/flexible.debug.js" type="text/javascript" charset="utf-8"></script>-->

	</head>

	<body>
		<header class="hui-header">
			<div id="hui-back"></div>
			<h1>选择社区</h1>
		</header>

		<div class="hui-wrap">
			<div class="hui-form-items">
			    <div class="hui-form-items-title"  id="communityCity"><span id="citycounity"></span><span class="hui-icons hui-icons-down2"></span></div>
			    <input type="text" class="hui-input hui-input-clear" id="searchCommunity" name="nickname" placeholder="点击搜索社区医院" />
			</div>
			<div class="hui-media-list" style="padding:10px;">
		        <ul id="communitylistUl">
	            	<li>
		                <a href="javascript:void(0)">
		                    <div class="hui-media-list-img">
		                    	<img src="../../img/3.jpg" />
		                    </div>
		                    <div class="hui-media-content">
		                        <h1>北京医科大学</h1>
		                        <!--<p>笑对人生，能穿透迷雾；笑对人生，能坚持到底；笑对人生，能化解危机；笑对人生，能照亮黑暗。</p>
		                        <p class="mainCommunity">主要科室：笑对人生，能穿透迷雾；笑对人生，能坚持到底；笑对人生，能化解危机；笑对人生，能照亮黑暗。</p>-->
		                        <span>距离：<i>1.2km</i></span>
		                    </div>
		                </a>
	            	</li>
		        </ul>
		    </div>
		</div>
		
		<script src="../../js/hui/hui.js" type="text/javascript" charset="utf-8"></script>
		<!--<script src="../../js/script/personcenter.js" type="text/javascript" charset="utf-8"></script>-->
		<script src="../../js/common/common.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var _Latitude = localStorage.getItem('Latitude');
			var _Longitude = localStorage.getItem('Longitude');
			var search = {
	    		'province':'',
	    		'city':'',
	    		'lng':_Longitude,
	    		'lat':_Latitude,
	    		'page':'1',
	    		'pagesize':'10'
	    	}
			
	    	 
	    	function goMyDoctor(data){
	    		console.log(JSON.stringify(data))
	    		localStorage.setItem('Latitude',data.location[1]);
				localStorage.setItem('Longitude',data.location[0]);
//				hui.getViewById("/html/locationCommunity/myDoctore.html").reload();
	    		hui.open('/html/locationCommunity/myDoctore.html');
	    	}
	    	
	    	function getbyposition(){
	    		console.log(JSON.stringify(search));
				hui.ajax({
				    type : 'POST',
				    url : globleobj['domain']+'/comhospital/getbyposition',
				    data : search,
				    beforeSend : function(){hui.loading();},
				    complete   : function(){hui.closeLoading();},
				    success : function(msg){
				    	console.log('msg:'+msg)
				    	var data = JSON.parse(msg)
				    	if(data.rc != 0){
				    		hui.toast(data.msg);
				    		return false; 
				    	}
				    	var options = '';
				    	for(var i= 0;i< data.list.length; i++){
				    		options += '<li onclick="goMyDoctor('+JSON.stringify(data.list[i]).replace(/\"/g,"'")+')">'
					    				+'<a href="javascript:void(0)">'
						    				+'<div class="hui-media-list-img">'
						    					+'<img src="'+data.list[i].banner+'" />'
						    				+'</div>'
						    				+'<div class="hui-media-content">'
						    					+'<h1>'+data.list[i].name+'</h1>'
//						    					+'<p>笑笑对人生，能照亮黑暗。</p>' 
//						    					+'<p class="mainCommunity">主暗。</p>'
						    					+'<span>距离：<i>'+(data.list[i].distances?data.list[i].distances.toFixed(2):"")+'</i></span>'
				    					+'</div></a>'
				    				+'</li>'
				    	} 
				    	hui('#communitylistUl').html(options)
				    },
				    error : function(e){
				        console.log(JSON.stringify(e));
				        hui.iconToast('读取消息失败', 'warn'); 
				    } 
				}); 
			}
	    	
			hui.ready(function(){
				//根据经纬度获取位置
				if(!localStorage.getItem('getcity')){
				    var map = new AMap.Map("container", {
				        resizeEnable: true,
						zoom: 18
				    }),    
				    lnglatXY = [_Longitude, _Latitude]; //已知点坐标
				    regeocoder();
				    function regeocoder() {  //逆地理编码
				        var geocoder = new AMap.Geocoder({
				            radius: 1000,
				            extensions: "all"
				        });        
				        geocoder.getAddress(lnglatXY, function(status, result) {
				            if (status === 'complete' && result.info === 'OK') {
				                geocoder_CallBack(result);
				            }
				        });        
				        var marker = new AMap.Marker({  //加点
				            map: map,
				            position: lnglatXY 
				        });
				        map.setFitView();
				    }
				    function geocoder_CallBack(data) {
				        var address = data.regeocode.addressComponent.province; //返回地址描述
				        console.log('定位地址'+address);
						search.city = address; 
				        hui('#citycounity').html(address);
				        next();
				    } 
				    
				}else{
					console.log(localStorage.getItem('getcity'))
					search.city = localStorage.getItem('getcity');
					hui('#citycounity').html(localStorage.getItem('getcity'));
					next();
				}
				
				function next(){
					
		    		console.log('search'+JSON.stringify(search));
					var searchCom = document.getElementById("searchCommunity");
					getbyposition();
				        
			        //选择社区
				 	hui('#communityCity').click(function(){
					    hui.open('/html/helthy/positioncity.html');
					});
					
					searchCom.oninput = function(){
						getbyposition();
					}
				}
			});
			
		</script>
	</body>
</html>